package com.cbn.ibcp.jishiting.gs.cms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cbn.ibcp.common.annotation.DataSource;
import com.cbn.ibcp.common.enums.DataSourceType;
import com.cbn.ibcp.jishiting.gs.cms.domain.CpVideo;
import com.cbn.ibcp.jishiting.gs.cms.enums.OssInjectStatusEnum;
import com.cbn.ibcp.jishiting.gs.cms.mapper.CpVideoMapper;
import com.cbn.ibcp.jishiting.gs.cms.service.ICpVideoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class CpVideoServiceImpl implements ICpVideoService {


    private final CpVideoMapper cpVideoMapper;


    @DataSource(DataSourceType.gs_cms)
    @Override
    public CpVideo getById(Long id) {
        if (Objects.isNull(id)) {
            return null;
        }
        return cpVideoMapper.selectById(id);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public List<CpVideo> getListByParam(Long albumId, Long videoId) {
        LambdaQueryWrapper<CpVideo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String albumIdStr = StrUtil.toStringOrNull(albumId);
        if (StrUtil.isNotBlank(albumIdStr)) {
            lambdaQueryWrapper.eq(CpVideo::getAlbumId, albumIdStr);
        }
        String videoIdStr = StrUtil.toStringOrNull(videoId);
        if (StrUtil.isNotBlank(videoIdStr)) {
            lambdaQueryWrapper.eq(CpVideo::getVideoId, videoIdStr);
        }
        return cpVideoMapper.selectList(lambdaQueryWrapper);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public CpVideo getByVideoId(String videoId) {
        if (Objects.isNull(videoId)) {
            return null;
        }
        LambdaQueryWrapper<CpVideo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CpVideo::getVideoId, videoId);
        return cpVideoMapper.selectOne(lambdaQueryWrapper);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public List<CpVideo> getListByVideoIdList(List<Long> videoIds) {
        if (CollUtil.isEmpty(videoIds)) {
            return null;
        }
        List<String> videoIdStrList = videoIds.stream().map(StrUtil::toStringOrNull).collect(Collectors.toList());
        LambdaQueryWrapper<CpVideo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CpVideo::getVideoId, videoIdStrList);
        return cpVideoMapper.selectList(lambdaQueryWrapper);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalCreateInjectStatistic(Date startTime, Date endTime, Integer ossInjectStatus) {
        log.info("getTotalCreateInjectStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<CpVideo> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(CpVideo::getCreateTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(CpVideo::getCreateTime, endTime);
        }
        if (Objects.nonNull(ossInjectStatus)) {
            queryWrapper.eq(CpVideo::getOssInjectStatus, ossInjectStatus);
        }
        return cpVideoMapper.selectCount(queryWrapper);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalCreateInjectFailureStatistic(Date startTime, Date endTime) {
        log.info("getTotalCreateInjectFailureStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<CpVideo> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(CpVideo::getCreateTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(CpVideo::getCreateTime, endTime);
        }
        queryWrapper.notIn(CpVideo::getOssInjectStatus, OssInjectStatusEnum.WAITING_INJECT.getCode(), OssInjectStatusEnum.INJECTING.getCode(), OssInjectStatusEnum.INJECT_SUCCESS.getCode());
        return cpVideoMapper.selectCount(queryWrapper);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalUpdateInjectStatistic(Date startTime, Date endTime, Integer ossInjectStatus) {
        log.info("getTotalCreateInjectStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<CpVideo> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(CpVideo::getModifyTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(CpVideo::getModifyTime, endTime);
        }
        if (Objects.nonNull(ossInjectStatus)) {
            queryWrapper.eq(CpVideo::getOssInjectStatus, ossInjectStatus);
        }
        return cpVideoMapper.selectCount(queryWrapper);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalUpdateInjectFailureStatistic(Date startTime, Date endTime) {
        log.info("getTotalUpdateInjectFailureStatistic param,startTime:{},endTime:{}", startTime, endTime);
        LambdaQueryWrapper<CpVideo> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(CpVideo::getModifyTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(CpVideo::getModifyTime, endTime);
        }
        queryWrapper.notIn(CpVideo::getOssInjectStatus, OssInjectStatusEnum.WAITING_INJECT.getCode(), OssInjectStatusEnum.INJECTING.getCode(), OssInjectStatusEnum.INJECT_SUCCESS.getCode());
        return cpVideoMapper.selectCount(queryWrapper);
    }
}
